<?php
namespace app\index\controller;
use think\Config;
use think\Controller;
use think\Cookie;
use think\Request;
use think\Session;

class Login extends Controller{

    function login(){
        if(Session::get('uid')){
            $this->redirect('/');
        }

        $appid = Config::get('AppID');
        $url = 'http://99.qikes.cn/public/index/login/getUserOpentId';
        $redirect_uri = urlencode($url);
        $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
        header("Location:".$url);exit;
    }

    public function http_curl($url,$type='get',$res='json',$arr=''){
        //1.初始化curl
        $ch = curl_init();
        //2.设置curl的参数
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($type == 'post') {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
        }
        //3.采集
        $output = curl_exec($ch);
        //4.关闭
        curl_close($ch);
        if ($res == 'json') {
            return json_decode($output,true);
        }
    }

    public function getUserOpentId(){
        //回调地址会传回一个code，则我们根据code去获取openid和授权获取到的access_token
        $code = $_GET['code'];
        $appid = Config::get('AppID');
        $secret = Config::get('AppSecret');
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
        $res = $this->http_curl($url);
        $access_token = $res['access_token'];
        $getopenid = $res['openid'];
        //获取用户授权信息
        $urltoc = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$getopenid."&lang=zh_CN";
        $resinfos = $this->http_curl($urltoc);
        $openid = $resinfos['openid'];
        $check_member = model('user')->where('openid',$openid)->find();
        if(!$check_member){
            //首次进入，则获取用户信息，插入数据库
            $info['openid'] = $openid;
            $info['nickname'] = $resinfos['nickname'];
            $info['img'] = $resinfos['headimgurl'];
            $info['createTime'] = time();
            $puid = Session::get('puid');
            $ppuid = Session::get('ppuid');
            if($puid){
                $info['puid'] = $puid;
            }
            if($ppuid){
                $info['ppuid'] = $ppuid;
            }
            //注册送积分
            $credit = getSettingValue('register_credit');
            $info['credit'] = $credit?$credit:0;
            $uid = model('user')->insertGetId($info);
            Session::set('uid', $uid);
            Cookie::set('uid', $uid);
        }
        else{
            Session::set('uid', $check_member->uid);
            Cookie::set('uid', $check_member->uid);
        }
        $fromUrl = Session::get('fromUrl');
        if($fromUrl){

            $this->redirect($fromUrl);
        }
        $this->redirect('/');

    }



    function loginOut(){

        Session::delete('uid');
        Cookie::delete('uid');
        $this->redirect('/');

    }






















}